/**
 * Created by 小润 on 2017/4/25.
 */
$(document).ready(function(){
    init();
    //申请加入家族
    $('.if-add .addfamily').on('click',function(){
    	if($(this).hasClass("btn-primary")){
        		alert('你已经加入该家族了！');
    	}else{
    		 familyAdd($(this));
    	}
            
    });
//  $('.if-add .btn-primary').on('click',function (){
//      alert('你已经加入该家族了！')
//  });
});

//添加家族
function init(){
    $('#familyAdd').on('click',function(){
    	$("#mask").css({
    		"opacity":"0.5",
    		"background":"#000000",
    		"display":"block"
    	})
        $('.family-add').show();
        $('#addCancel').click(function(){
            $('.family-add').hide();
            $("#mask").css({
	    		"opacity":"1",
	    		"background":"#F9F9F9",
	    		"display":"none"
	    	})
        });
        $('#addSure').on('click',function(){
            var $fName = $('#fName').val();
            var $fDesp = $('#fDesp').val();
            if($fName == ''||$fDesp == ''){
                alert('家族名称、描述不能为空！');
                return false;
            }else{
 /*               $.ajax({
                    type: "post",
                    url: 'http://192.168.0.107:8081/api/v1/family/create',
                    data: {
                        fName:$fName,
                        fDesp:$fDesp
                    },
                    success: function (data) {
                        alert('家族添加成功！');
                        console.log(data);
                        $(".create li").remove()
 						$(".create").append("<li><div class='container'><div class='row'>"
				        +"<div class='col-xs-4 family-image'><img src='../images/add-head.png' alt=''/>"
				        +"</div><div class='col-xs-4 family-detail'><p class='create_name'>"+$fName+"</p>"
				        +"<p class='create_desc'>"+$fDesp+"</p></div></div></div></li>")
				        
				        $('.family-add').hide();
				      	$('#fName').val("")
				        $('#fDesp').val("")
                        
                    },
                    error: function (msg) {
                        alert('请求失败');
                        console.log(msg)
                    }
                });*/
				var url='http://192.168.0.107:8081/api/v1/family/create'
				var data = {
                      "fName":$fName,
                      "fDesp":$fDesp
                  }
				$.post(url,data,function(result){
					alert('家族添加成功！');
					console.log(result.message)
					$(".create li").remove()
					$(".create").append("<li><div class='container'><div class='row'>"
			        +"<div class='col-xs-4 family-image'><img src='../images/add-head.png' alt=''/>"
			        +"</div><div class='col-xs-4 family-detail'><p class='create_name'>"+$fName+"</p>"
			        +"<p class='create_desc'>"+$fDesp+"</p></div></div></div></li>")
			        
			        $('.family-add').hide();
			      	$('#fName').val("")
			        $('#fDesp').val("")
				},JSON)
            }
        })
    })
}

function familyAdd(obj) {
    var $f_id = $(this).siblings().val();
//  var $fp_id;  //接口还有问题，先不传值
    $.ajax({
        type: "post",
        url: 'http://192.168.0.107:8081/api/v1/family/apply',
        data: {
            fName:$f_id,
//          fDesp:$fp_id
        },
        success: function (data) {
            alert('加入成功！');
            obj.addClass("btn-primary").removeClass("btn-default").html("申请中");//加入家族成功后样式
            console.log(data.result);
            
           
        },
        error: function (msg) {
        	
            alert('请求失败');
            console.log(msg)
        }
    });
}



